package com.chuanyang.driverh.mainorder;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainorder.adapter.GoodsOwnerCommentsdapter;
import com.chuanyang.driverh.mainorder.adapter.OffenTranslateLinesAdapter;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ShipperMsgData;
import com.chuanyang.driverh.responseresult.ShipperMsgResult;
import com.chuanyang.driverh.responseresult.WaybillDetailsData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.SubscribeEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.CustomMeasureHeightListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

public class GoodOwnerInfoActivity extends BaseTitleActivity {

    private HashMap params;
    private ArrayList offerlines;
    private ArrayList<ShipperMsgData.DriverEvaluateMsgsBean> goodownercomments;
    private GoodsOwnerCommentsdapter commentAdapter;
    private OffenTranslateLinesAdapter linesAdapter;
    private OrderDetailData orderDetailData;
    private ImageView iv_oredrinfo_peopleicon;
    private TextView tv_oredrinfo_peoplename;
    private TextView tv_oredrinfo_phone;
    private CustomMeasureHeightListView lv_goodownerinfo_offenlines;
    private CustomMeasureHeightListView lv_goodownerinfo_comments;
    private Intent intent;
    private String shipperId;
    private int subscriptionStatus;
    private TextView tv_oredrinfo_dingyue;
//    private LinearLayout ll_oredrinfo_goodownerdiv;
    private ImageView iv_oredrinfo_phoneicon;

    private Dialog dialog;
    @Override
    protected int getContentResId() {
        return R.layout.order_act_goodsownerinfo;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
//        ll_oredrinfo_goodownerdiv = (LinearLayout) findViewById(R.id.ll_oredrinfo_goodownerdiv);
        iv_oredrinfo_peopleicon = (ImageView) findViewById(R.id.iv_oredrinfo_peopleicon);
        tv_oredrinfo_peoplename = (TextView) findViewById(R.id.tv_oredrinfo_peoplename);
        tv_oredrinfo_dingyue = (TextView) findViewById(R.id.tv_oredrinfo_dingyue);//订阅
        tv_oredrinfo_phone = (TextView) findViewById(R.id.tv_oredrinfo_phone);
        iv_oredrinfo_phoneicon = (ImageView) findViewById(R.id.iv_oredrinfo_phoneicon);
        lv_goodownerinfo_offenlines = (CustomMeasureHeightListView) findViewById(R.id.lv_goodownerinfo_offenlines);
        lv_goodownerinfo_comments = (CustomMeasureHeightListView) findViewById(R.id.lv_goodownerinfo_comments);
    }

    @Override
    public void initData() {
        setBackImg();
        getTitleTextView().setText("客户信息");
        intent = getIntent();
        fillView();
    }

    @Override
    protected void initListener() {
        iv_oredrinfo_phoneicon.setOnClickListener(this);
    }


    private void fillView() {
        tv_oredrinfo_peoplename.setText("---");
        offerlines = new ArrayList();
        goodownercomments = new ArrayList();
//        for (int i=0;i<10;i++)
//        {
//            CommentBean bean=new CommentBean();
//            goodownercomments.add(bean);
//        }
        commentAdapter = new GoodsOwnerCommentsdapter(this, goodownercomments);
        linesAdapter = new OffenTranslateLinesAdapter(offerlines, this);
        lv_goodownerinfo_comments.setAdapter(commentAdapter);
        lv_goodownerinfo_offenlines.setAdapter(linesAdapter);
        if (intent.getSerializableExtra("data") instanceof OrderDetailData) {
            orderDetailData = (OrderDetailData) intent.getSerializableExtra("data");
            BitmapUtil.showImgForNetPath( orderDetailData.getUserImage(),iv_oredrinfo_peopleicon);
            tv_oredrinfo_phone.setText(orderDetailData.getPhone());
            if (!TextUtils.isEmpty(orderDetailData.getUsername())) {
                tv_oredrinfo_peoplename.setText(orderDetailData.getUsername());
            }
            shipperId = orderDetailData.getUserId() + "";
            subscriptionStatus = orderDetailData.getSubscriptionStatus();
        }
        else {
            WaybillDetailsData waybillDetailsData = (WaybillDetailsData) intent.getSerializableExtra("data");
            BitmapUtil.showImgForNetPath(  waybillDetailsData.getUserImage(),iv_oredrinfo_peopleicon);
            tv_oredrinfo_phone.setText(waybillDetailsData.getPhone());
            if (!TextUtils.isEmpty(waybillDetailsData.getUsername())) {
                tv_oredrinfo_peoplename.setText(waybillDetailsData.getUsername());
            }
            shipperId = waybillDetailsData.getUserId() + "";
            subscriptionStatus = waybillDetailsData.getSubscriptionStatus();
        }
        if (subscriptionStatus == 1) {
            tv_oredrinfo_dingyue.setText("已订阅");
            getRightTextView().setText("取消订阅");
        } else if (subscriptionStatus == 2) {
            tv_oredrinfo_dingyue.setText("未订阅");
            getRightTextView().setText("订阅");
        }

        getData();
    }

    private void getData() {
        DialogUtil.showLoadingDialog(activityContext,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("userId",shipperId);
        MyHttpUtil.selectShipperMsg(activityContext,params, new MyCallback<ShipperMsgResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ShipperMsgResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData().getDriverEvaluateMsgs() != null && result.getData().getDriverEvaluateMsgs().size() > 0) {
                        goodownercomments = (ArrayList<ShipperMsgData.DriverEvaluateMsgsBean>) result.getData().getDriverEvaluateMsgs();
                        commentAdapter.setObjects(goodownercomments);
                    }
                    if (result.getData().getShipperLines() != null && result.getData().getShipperLines().size() > 0) {
                        offerlines = (ArrayList) result.getData().getShipperLines();
                        linesAdapter.setList(offerlines);
                    }
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }



    @Override
    protected void baseGoEnsure() {
        if (subscriptionStatus == 1) {
            deleteDsByDriverAndShipper();
        } else {
            addDriverSubscription();
        }


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case  R.id.iv_oredrinfo_phoneicon:
                if(!TextUtils.isEmpty(tv_oredrinfo_phone.getText().toString())) {
                    dial(tv_oredrinfo_phone.getText().toString());
                }

                break;
        }
    }


    private void dial(final String tel){
        dialog= DialogUtil.myDialog(activityContext, "拨打电话", "取消", "确定","确定拨打："+tel+"?", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.dialog_btn_left:
                        break;
                    case R.id.dialog_btn_right:
                        DialogUtil.dial(activityContext,tel);
                       /* Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel));
                        try {
                            if (PermissoinUtil.checkAndRequestPermission(activityContext, android.Manifest.permission.CALL_PHONE, "请给应用添加拨打电话的权限！"))
                                startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }*/
                        break;
                }
                dialog.cancel();
            }
        });
    }

    private void deleteDsByDriverAndShipper() {
        DialogUtil.showLoadingDialog(activityContext,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("shipperId", shipperId);
        MyHttpUtil.deleteDsByDriverAndShipper(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activityContext,"取消订阅成功");
                    tv_oredrinfo_dingyue.setText("未订阅");
                    getRightTextView().setText("订阅");
                    subscriptionStatus = 2;
                    EventBus.getDefault().post(new SubscribeEvent(true));
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });


    }


    private void addDriverSubscription() {
        DialogUtil.showLoadingDialog(activityContext,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("shipperId", shipperId);
        MyHttpUtil.addDriverSubscription(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activityContext,"订阅成功");
                    tv_oredrinfo_dingyue.setText("已订阅");
                    getRightTextView().setText("取消订阅");
                    subscriptionStatus = 1;
                    EventBus.getDefault().post(new SubscribeEvent(true));
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
